package com.me.math {
	import flash.geom.Point;

	import mx.charts.PieChart;

	public class MMath {
		public function MMath() {
		}

		private static function cross1(p1:Point, p2:Point):Number {
//			p1(x1, y1) X p2(x2, y2) = x1y2 – y1x2
			return p1.x * p2.y - p1.y * p2.x;
		}

		public static function tracePoint(p:Point):void {
			trace(p.x + ":" + p.y);
		}

		public static function cross(a:Point, b:Point, c:Point, d:Point):Boolean {
			var a1:Number = cross2(a, b, c);
			var a2:Number = cross2(a, c, d);
			var a3:Number = cross2(a, b, d);
			if (a1 >= 0) {
				return a2 >= 0 && a3 >= 0;
			} else {
				return a2 < 0 && a3 < 0;
			}
		}

		/**
		 * 判断点a与bc的叉乘
		 *
		 * 	@param a
		 *  @param b
		 *  @param c
		 */
		private static function cross2(a:Point, b:Point, c:Point):Number {
			var ab:Point = new Point();
			var ac:Point = new Point();
			ab.x = -b.x + a.x;
			ab.y = -b.y + a.y;
			ac.x = -c.x + a.x;
			ac.y = -c.y + a.y;
			return cross1(ab, ac);
		}

		//// 由给定的三个顶点的坐标，计算三角形面积。
		public static function triangleArea(pos1:Point, pos2:Point, pos3:Point):Number {
			return Math.abs((pos1.x * pos2.y + pos2.x * pos3.y + pos3.x * pos1.y - pos2.x * pos1.y - pos3.x * pos2.y - pos1.x * pos3.y) / 2.0);
		}

		// 判断点pos是否在指定的三角形内。
		public static function inTriangle(pos:Point, posA:Point, posB:Point, posC:Point):Boolean {
			var Area:Number = triangleArea(posA, posB, posC);
			var area:Number = triangleArea(pos, posA, posB);
			area += triangleArea(pos, posA, posC);
			area += triangleArea(pos, posB, posC);
			var epsilon:Number = 0.0001; // 由于浮点数的计算存在着误差，故指定一个足够小的数，用于判定两个面积是否(近似)相等。
			if (Math.abs(Area - area) < epsilon) {
				return true;
			}
			return false;
		}
	}
}
